/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;
import bean.Estabelecimento;
import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.GerenciarDB;

/**
 *
 * @author Rafaella
 */
public class JanelaCadastrarEst extends javax.swing.JFrame {

    /**
     * Creates new form JanelaCadastrarEst
     */
    public JanelaCadastrarEst() {
        initComponents();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        Nome = new javax.swing.JLabel();
        NomeLoja = new javax.swing.JTextField();
        CNPJ = new javax.swing.JLabel();
        CNPJLoja = new javax.swing.JFormattedTextField();
        Telefone = new javax.swing.JLabel();
        TelLoja = new javax.swing.JFormattedTextField();
        ErroNome = new javax.swing.JLabel();
        CNPJErro = new javax.swing.JLabel();
        TelErro = new javax.swing.JLabel();
        ErroCampoVazio = new javax.swing.JLabel();
        BotaoCadastrarLoja = new javax.swing.JButton();
        BotaoApagarCamposLoja = new javax.swing.JButton();
        BotaoCancelarLoja = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setText("Cadastrar Loja");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 40, -1, -1));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Nome.setText("Nome:");
        jPanel1.add(Nome, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        NomeLoja.setToolTipText("Nome do Estabelecimento");
        jPanel1.add(NomeLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 20, 500, -1));

        CNPJ.setText("CNPJ:");
        jPanel1.add(CNPJ, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, -1, -1));

        CNPJLoja.setToolTipText("CNPJ do estabelecimento");
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##.###.###/####-##");  
            CNPJLoja = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        jPanel1.add(CNPJLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 50, 127, -1));

        Telefone.setText("Telefone:");
        jPanel1.add(Telefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, -1, -1));

        TelLoja.setToolTipText("Telefone do estabelecimento");
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("(##)####-####");  
            TelLoja = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        jPanel1.add(TelLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 90, 108, -1));

        ErroNome.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        ErroNome.setForeground(new java.awt.Color(255, 0, 0));

        CNPJErro.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        CNPJErro.setForeground(new java.awt.Color(255, 0, 0));

        TelErro.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        TelErro.setForeground(new java.awt.Color(255, 0, 0));

        ErroCampoVazio.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(ErroCampoVazio, new org.netbeans.lib.awtextra.AbsoluteConstraints(454, 66, -1, 11));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 660, 150));

        BotaoCadastrarLoja.setText("Confirmar");
        BotaoCadastrarLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoCadastrarLojaActionPerformed(evt);
            }
        });
        getContentPane().add(BotaoCadastrarLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 380, 100, 30));

        BotaoApagarCamposLoja.setText("Limpar");
        BotaoApagarCamposLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoApagarCamposLojaActionPerformed(evt);
            }
        });
        getContentPane().add(BotaoApagarCamposLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 380, 100, 30));

        BotaoCancelarLoja.setText("Cancelar");
        BotaoCancelarLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoCancelarLojaActionPerformed(evt);
            }
        });
        getContentPane().add(BotaoCancelarLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 380, 100, 30));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-728)/2, (screenSize.height-514)/2, 728, 514);
    }// </editor-fold>//GEN-END:initComponents

    private void BotaoApagarCamposLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoApagarCamposLojaActionPerformed
        // TODO add your handling code here:
        NomeLoja.setText("");
        CNPJLoja.setText("");
        TelLoja.setText("");
    }//GEN-LAST:event_BotaoApagarCamposLojaActionPerformed

    private void BotaoCancelarLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoCancelarLojaActionPerformed
        // TODO add your handling code here:
        this.dispose();
        JanelaInicioAdmin frame = new JanelaInicioAdmin();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }//GEN-LAST:event_BotaoCancelarLojaActionPerformed

    private void BotaoCadastrarLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoCadastrarLojaActionPerformed
        // TODO add your handling code here:
         String nomeLoja = NomeLoja.getText();
         String cnpj = CNPJLoja.getText();
         cnpj = cnpj.replaceAll("[.]","");
         cnpj = cnpj.replaceAll("[/]","");  
         cnpj = cnpj.replaceAll("[-]",""); 
         String telLoja = TelLoja.getText();
         telLoja = (telLoja.replaceAll("[(]",""));
         telLoja = telLoja.replaceAll("[)]","");
         telLoja = telLoja.replaceAll("[-]","");
        
    
         if (((nomeLoja.isEmpty())  || ("              ".equals(cnpj)) || ("          ".equals(telLoja)) ||(cnpj.isEmpty())||(telLoja.isEmpty()))==true){
              ErroCampoVazio.setText("Existem campos vazios.");
              
              if (nomeLoja.isEmpty()==true){
                   //ErroNome.setText("*");
                  ErroCampoVazio.setText("Existem campos vazios.");
             
              }else if (cnpj.isEmpty()==true){
                   //CNPJErro.setText("*");
                  ErroCampoVazio.setText("Existem campos vazios.");
              }if(telLoja.isEmpty()==true){
                  // TelErro.setText("*");
                  ErroCampoVazio.setText("Existem campos vazios.");
              }
         }
         else{
             try {
                 GerenciarDB estabelecimento = new GerenciarDB();
                 Estabelecimento shop = new Estabelecimento (cnpj, nomeLoja);
                 estabelecimento.addEstabelecimento(shop);
                 JanelaInicioAdmin frame = new JanelaInicioAdmin();
                 frame.setLocationRelativeTo(null);
                 frame.setVisible(true);
                 this.dispose();
                 
                 

            } catch (Exception ex) {
                Logger.getLogger(JanelaCadastrarEst.class.getName()).log(Level.SEVERE, null, ex);
            }
             
         }
    }//GEN-LAST:event_BotaoCadastrarLojaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JanelaCadastrarEst().setVisible(true);          
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotaoApagarCamposLoja;
    private javax.swing.JButton BotaoCadastrarLoja;
    private javax.swing.JButton BotaoCancelarLoja;
    private javax.swing.JLabel CNPJ;
    private javax.swing.JLabel CNPJErro;
    private javax.swing.JFormattedTextField CNPJLoja;
    private javax.swing.JLabel ErroCampoVazio;
    private javax.swing.JLabel ErroNome;
    private javax.swing.JLabel Nome;
    private javax.swing.JTextField NomeLoja;
    private javax.swing.JLabel TelErro;
    private javax.swing.JFormattedTextField TelLoja;
    private javax.swing.JLabel Telefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}

